#include <ui_mainwindow.h>
#include <mainwindow.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&wizard,SIGNAL(accepted()),this,SLOT(on_OpenDataWizard_accepted()));
    connect(this,SIGNAL(renderAddComplete(vtkRenderer*)),this,SLOT(on_renderAddComplete(vtkRenderer*)));
    
	ui->actionRedo_last_selection->setEnabled(false);
	ui->actionUndo_last_selection->setEnabled(false);

	ColorMap::getInstance()->setLUT_Linear_BlueToYellow();
	
	centralWindow = new CentralWindow(this);
	centralWindow->setWindowFlags(Qt::Widget);
	setCentralWidget(centralWindow);

	scatterDock = new QDockWidget(tr("Scatter plot"),this);
	scatterDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	hierarchDock = new QDockWidget(tr("Hierarchy View"),this);
	hierarchDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	taiyunDock = new QDockWidget(tr("Matrix Bitmap"),this);
	taiyunDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	filterDock = new QDockWidget(tr("Data Filtering"),this);
	filterDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	infoDock = new QDockWidget(tr("Selection information"),this);
	infoDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);

    scatterplotWidget = new ScatterPlotView(this);
	filterView = new FilterView(this);
    hierarchicalWidget= new HierarchicalBundleView(this);
    taiyunWidget = new TaiyunView(this);
    infoView = new InfoView(this);

	individualSlider = new QSlider(ui->toolBar);
	individualInfoLabel = new QLabel(ui->toolBar);
	individualSelectedLabel = new QLabel(ui->toolBar);

	individualSlider->setOrientation(Qt::Horizontal);
	individualSlider->setTickPosition(QSlider::TicksBelow);
	individualSlider->setTickInterval(1);

	individualInfoLabel->setText(tr("Select the data to render: "));
	individualSelectedLabel->setText(tr("Subject %1").arg(1));

	averageButton = new QRadioButton(ui->toolBar);
	individualButton = new QRadioButton(ui->toolBar);
	averageButton->setText(tr("Subjects average"));
	individualButton->setText(tr("Individual subject: "));
	individualButton->setChecked(true);

	ui->toolBar->addWidget(individualInfoLabel);

	ui->toolBar->addWidget(averageButton);
	ui->toolBar->addWidget(individualButton);

	ui->toolBar->addWidget(individualSlider);
	ui->toolBar->addWidget(individualSelectedLabel);

	plotGroup.addButton(averageButton);
	plotGroup.addButton(individualButton);

	connect(individualSlider,SIGNAL(valueChanged(int)),this,SLOT(on_individualChanged(int)));
	connect(averageButton,SIGNAL(toggled(bool)),this,SLOT(on_averageButton_toggled(bool)));

	regionWidget = new RegionView(this);
	linkWidget = new LinkView(this);

	scatterDock->setWidget(scatterplotWidget);
	hierarchDock->setWidget(hierarchicalWidget);
	taiyunDock->setWidget(taiyunWidget);
	filterDock->setWidget(filterView);
	infoDock->setWidget(infoView);

	centralWindow->addDockWidget(Qt::BottomDockWidgetArea,scatterDock);
	addDockWidget(Qt::RightDockWidgetArea,infoDock);
    addDockWidget(Qt::RightDockWidgetArea,filterDock);
	//addDockWidget(Qt::RightDockWidgetArea,hierarchDock);
    centralWindow->addDockWidget(Qt::BottomDockWidgetArea,taiyunDock);
	centralWindow->addDockWidget(Qt::BottomDockWidgetArea,hierarchDock);
	
	style2 = View3DInteractorStyle::New();
	style1 = View3DInteractorStyle::New();
	connect(style1,SIGNAL(viewPointChanged(vtkRenderer *)),style2,SLOT(on_viewPointChanged(vtkRenderer *)));
	connect(style2,SIGNAL(viewPointChanged(vtkRenderer *)),style1,SLOT(on_viewPointChanged(vtkRenderer *)));
	
	regionWidget->setInteractorStyle(style1);
	linkWidget->setInteractorStyle(style2);
	
	centralWindow->GetLayout()->addWidget(regionWidget);
	centralWindow->GetLayout()->addWidget(linkWidget);

	meanMatrix = NULL;
	medianMatrix = NULL;
    aTemplate = NULL;
    network = NULL;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_individualChanged(int ind)
{
	if(network)
	{
		individualSelectedLabel->setText(tr("Individual %1").arg(ind));
		network->setCurrentIndividualMatrix(individualMatrices.at(ind-1));
		filterView->on_matrixChanged();
	}
}

void MainWindow::on_averageButton_toggled(bool checked)
{
	individualSlider->setEnabled(!checked);
	network->setCurrentIndividualMatrix(checked?meanMatrix:individualMatrices.at(individualSlider->value()-1));
}

void MainWindow::on_actionOpen_dataset_triggered()
{
    wizard.restart();
    wizard.show();
}

void MainWindow::on_OpenDataWizard_accepted()
{
	meanMatrix = wizard.GetMeanMatrix();
	medianMatrix = wizard.GetMedianMatrix();
	individualMatrices = wizard.GetIndividualsMatrices();

	aTemplate = wizard.GetTemplate();
    network = wizard.GetNetwork();

	individualSlider->setMinimum(1);
	individualSlider->setMaximum(individualMatrices.size());
	individualSlider->setValue(1);	
	
    hierarchicalWidget->setNetwork(network);
    scatterplotWidget->setNetwork(network);
	filterView->setNetwork(network);
	regionWidget->setNetwork(network);
	linkWidget->setNetwork(network);
	taiyunWidget->setNetwork(network);
	infoView->setNetwork(network);
	
	emit matrixChanged();	

	connect(network,SIGNAL(selectionChanged()),this,SLOT(on_selectionChanged()));
}

void MainWindow::on_renderAddComplete(vtkRenderer *render)
{
    render->ResetCamera();
	render->GetRenderWindow()->Render();
}

void MainWindow::on_actionView_Edit_data_matrix_triggered()
{
	matrixDialog.setMatrix(meanMatrix);
	matrixDialog.setAnatomicalTemplate(aTemplate);
	matrixDialog.show();
}

void MainWindow::on_actionBlue_to_Yellow_triggered()
{
	ColorMap::getInstance()->setLUT_BlueToYellow();
	emit colormapChanged();
}

void MainWindow::on_actionLinear_Blue_to_Yellow_triggered()
{
	ColorMap::getInstance()->setLUT_Linear_BlueToYellow();
	emit colormapChanged();
}

void MainWindow::on_actionLinear_Black_to_White_triggered()
{
	ColorMap::getInstance()->setLUT_Linear_BlackToWhite();
	emit colormapChanged();
}

void MainWindow::on_actionLinear_Heat_triggered()
{
	ColorMap::getInstance()->setLUT_Linear_Heat();
	emit colormapChanged();
}

void MainWindow::on_actionExit_triggered()
{
	QApplication::exit(0);
}

void MainWindow::on_actionSelect_all_triggered()
{
	if( !aTemplate || !network )
		return;

	SelectedLinks links;
	QVector<AnatomicalLink*> all= network->getLinks();
	
	for(int i = 0; i< all.size();i++)
		links.push_back(all.at(i));

	scatterplotWidget->setSelection(links);
}

void MainWindow::on_actionDeselect_triggered()
{
	if( !aTemplate || !network )
		return;

	SelectedLinks links;

	scatterplotWidget->setSelection(links);
}

void MainWindow::on_actionRedo_last_selection_triggered()
{
	if(!network)
		return;

	network->redoSelection();
}

void MainWindow::on_actionUndo_last_selection_triggered()
{
	if(!network)
		return;

	network->undoSelection();
}

void MainWindow::on_selectionChanged()
{
	if(!network)
		return;

	ui->actionRedo_last_selection->setEnabled(network->redoPossible());
	ui->actionUndo_last_selection->setEnabled(network->undoPossible());
}

void MainWindow::on_actionAlways_show_context_triggered()
{
	regionWidget->setShowContext(ui->actionAlways_show_context->isChecked());
}